import Vue from 'vue'
import Router from 'vue-router'
import Login from './components/Login.vue'
import Home from './components/Home.vue'
import Welcome from './components/Welcome.vue'

import category from './components/books/Category.vue'

import record from './components/record/RecordList.vue'
import resgister from './components/resgister.vue'

import bookuser from './components/readers/BookUser.vue'

import user from './components/user/User.vue'
import { setStore, getStore, removeStore } from './components/util/store'
Vue.prototype.setStore = setStore
Vue.prototype.getStore = getStore
Vue.prototype.removeStore = removeStore
Vue.use(Router)

const router = new Router({
  routes: [
    { path: '/', redirect: '/login' },
    { path: '/login', component: Login },
    { path: '/resgister', component: resgister },
    {
      path: '/home',
      component: Home,
      redirect: '/welcome',
      children: [
        { path: '/welcome', component: Welcome },
        { path: '/bookList', component: () => import('./components/books/BookList.vue') },
        {path:'/categoryList',component:category},
        {path:'/recordList',component:record},
        { path: '/borrowList', component: () => import('./components/record/BorrowList.vue') },
        {path:'/bookuserList',component:bookuser},
        {path:'/userList',component:user},
        { path: '/myBorrowList', component: () => import('./components/readers/BorrowSituation.vue') },
        { path: '/historyList', component: () => import('./components/readers/HistoryList.vue') },
      ]
    }
  ]
})

// 挂载路由导航守卫
router.beforeEach((to, from, next) => {
  // to 将要访问的路径
  // from 代表从哪个路径跳转而来
  // next 是一个函数，表示放行
  //     next()  放行    next('/login')  强制跳转
  // if (to.path === '/resgister') return next('/resgister')
  if (to.path === '/login') return next()
  // 获取token
  const tokenStr = window.sessionStorage.getItem('token')
  if (!tokenStr) return next('/login')
  next()
})

export default router
